<template>
  <div class="app-container">
    <header class="app-header">
      <div class="logo">HotelBooking</div>
      <nav class="main-nav">
        <router-link to="/">首页</router-link>
        <template v-if="!isAuthenticated">
          <router-link to="/login">登录</router-link>
        </template>
        <template v-else>
          <div class="user-dropdown" @click="toggleDropdown">
            <img :src="user.avatar || defaultAvatar" class="user-avatar" alt="用户头像">
            <span class="username">{{ user.username }}</span>
            <transition name="fade">
              <div v-if="showDropdown" class="dropdown-menu">
                <router-link to="/orders" class="dropdown-item">
                  <el-icon><Document /></el-icon> 我的订单
                </router-link>
                <router-link to="/profile" class="dropdown-item">
                  <el-icon><User /></el-icon> 个人资料
                </router-link>
                <div class="dropdown-item" @click="handleLogout">
                  <el-icon><SwitchButton /></el-icon> 退出登录
                </div>
              </div>
            </transition>
          </div>
        </template>
      </nav>
    </header>
    
    <main class="app-main">
      <router-view />
    </main>
    
    <footer class="app-footer">
      © 2023 HotelBooking - 类似Booking的酒店预订平台
    </footer>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { Document, User, SwitchButton } from '@element-plus/icons-vue'
import defaultAvatar from '@/assets/default-avatar.png'

const store = useStore()
const router = useRouter()
const showDropdown = ref(false)

// 计算属性
const isAuthenticated = computed(() => store.getters.isAuthenticated)
const user = computed(() => store.getters.userInfo)

// 方法
const toggleDropdown = () => {
  showDropdown.value = !showDropdown.value
}

const handleLogout = async () => {
  try {
    await store.dispatch('logout')
    router.push('/login')
    showDropdown.value = false
  } catch (error) {
    console.error('退出登录失败:', error)
  }
}

// 点击外部关闭下拉菜单
const handleClickOutside = (event) => {
  const dropdown = document.querySelector('.user-dropdown')
  if (dropdown && !dropdown.contains(event.target)) {
    showDropdown.value = false
  }
}

onMounted(() => {
  document.addEventListener('click', handleClickOutside)
  return () => {
    document.removeEventListener('click', handleClickOutside)
  }
})
</script>

<style scoped>
.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.app-header {
  background: #003580;
  color: white;
  padding: 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
}

.logo {
  font-size: 1.5rem;
  font-weight: bold;
}

.main-nav {
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

.main-nav a {
  color: white;
  text-decoration: none;
  transition: color 0.3s;
}

.main-nav a:hover {
  color: #b3d4ff;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  position: relative;
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid rgba(255, 255, 255, 0.3);
}

.username {
  font-weight: 500;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  min-width: 160px;
  z-index: 1000;
  margin-top: 5px;
  overflow: hidden;
}

.dropdown-item {
  padding: 10px 16px;
  color: #333;
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  transition: background-color 0.3s;
}

.dropdown-item:hover {
  background-color: #f5f5f5;
}

.dropdown-item .el-icon {
  font-size: 16px;
}

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s, transform 0.3s;
}

.fade-enter-from, .fade-leave-to {
  opacity: 0;
  transform: translateY(-10px);
}

.app-main {
  flex: 1;
  padding: 1rem;
}

.app-footer {
  background: #f5f5f5;
  padding: 1rem;
  text-align: center;
}
</style>